using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PassiveItemListDisplay : MonoBehaviour {
	
	public GameObject itemDisplayTemplate;
	
	List<GameObject> itemDisplays;
	
	public UITable table;
	
	public bool toggled = false;
	
	InventoryItem sItem;

	// Use this for initialization
	void Start () {
		
		itemDisplayTemplate.SetActiveRecursively(false);
		
		itemDisplays = new List<GameObject>();
		
		UIButtonMessage message = gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "ToggleItemDisplays";
		
		table.repositionNow = true;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void ToggleItemDisplays()
	{
		if(!toggled)
		{
			itemDisplayTemplate.SetActiveRecursively(true);
			
			List<InventoryItem> sortedList = SheetManager.activeCharacter.inventories.FindAll(i => i.slot == "None");
			
			sortedList.Sort((w1,w2) => w1.name.CompareTo(w2.name));
			
			GameObject go = new GameObject();
			go.transform.parent = transform.parent;
			go.transform.localPosition = Vector3.zero;
			go.transform.localScale = Vector3.one;
			go.name = itemDisplayTemplate.name;
			itemDisplays.Add(go);
			
			go = NGUITools.AddButton(go, 30, "Manage Inventory");
			UIButtonMessage message = go.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "ManageInventory";
			go.transform.localPosition = Vector3.right * ((BoxCollider)go.collider).size.x/2;
			
			foreach(InventoryItem pItem in sortedList)
			{
				go = Instantiate(itemDisplayTemplate) as GameObject;
				go.transform.parent = transform.parent;
				go.transform.localPosition = itemDisplayTemplate.transform.localPosition;
				go.transform.localScale = itemDisplayTemplate.transform.localScale;
				go.name += pItem.name;
				
				ItemListDisplayTemplate template = go.GetComponent<ItemListDisplayTemplate>();
				template.iname.text = pItem.name;
				template.info.text = pItem.protectiveItemDescription;
				if(template.slot != null)
					template.slot.text = pItem.slot;
				template.iname.maxHeight = 40;
				template.pItem = pItem;
				
				template.count.text = pItem.quantity.ToString();
				
				message = go.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "DisplayItemInfo";
				
				message = template.minusButton.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "ReduceQuantity";
				
				message = template.plusButton.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "IncreaseQuantity";
				
				itemDisplays.Add(go);
			}
			
			itemDisplayTemplate.SetActiveRecursively(false); 
			
			table.repositionNow = true;
			toggled = true;
		}
		else
		{
			foreach(GameObject go in itemDisplays)
			{
				Destroy(go);
			}
			itemDisplays.Clear();
			table.repositionNow = true;
			toggled = false;
		}
	}
	
	void DisplayItemInfo(GameObject target)
	{
		InventoryItem i = target.GetComponent<ItemListDisplayTemplate>().pItem;
		
		if(i as WeaponItem != null)
		{
			GameObject g = Instantiate(GameReference.reference.NewWeaponWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			BrowserManager.currentNewTransform = g.transform;
			g.transform.localPosition = BrowserManager.CurrentIndex;
			
			WeaponEditorMenu w = g.GetComponent<WeaponEditorMenu>();
			w.target = gameObject;
			w.functionName = "Do Nothing";
			w.LoadItemDisplay(i.name, true);
		}
		else
		{
			GameObject g = Instantiate(GameReference.reference.NewItemWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			BrowserManager.currentNewTransform = g.transform;
			g.transform.localPosition = BrowserManager.CurrentIndex;
			
			ItemEditorMenu w = g.GetComponent<ItemEditorMenu>();
			w.target = gameObject;
			w.functionName = "Do Nothing";
			w.LoadItemDisplay(i.name, true);
		}
	}
	
	void ManageInventory()
	{
		BrowserManager.CreateItemsBrowser(gameObject, "AddItems", new List<string>());
	}
	
	void ReduceQuantity(GameObject target)
	{
		ItemListDisplayTemplate template = target.transform.parent.GetComponent<ItemListDisplayTemplate>();
		
		sItem = template.pItem;
		
		if(template.pItem.quantity > 1)
		{
			template.pItem.quantity--;
			template.count.text = template.pItem.quantity.ToString();
			
			SheetManager.SaveActiveCharacter(false);
		}
		else
		{
			StartCoroutine(RemoveItem());
		}
	}
	
	IEnumerator RemoveItem()
	{
		List<bool> outBools = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to remove " + sItem.name + " from your inventory?", outBools));
		
		if(outBools[0])
		{
			if(sItem as WeaponItem != null && SheetManager.activeCharacter.weapons.Contains(sItem as WeaponItem))
				SheetManager.activeCharacter.weapons.Remove(sItem as WeaponItem);
			
			if(SheetManager.activeCharacter.inventories.Contains(sItem))
				SheetManager.activeCharacter.inventories.Remove(sItem);
			
			ToggleItemDisplays();
			ToggleItemDisplays();
			
			SheetManager.SaveActiveCharacter(false);
		}
	}
	
	void IncreaseQuantity(GameObject target)
	{
		ItemListDisplayTemplate template = target.transform.parent.GetComponent<ItemListDisplayTemplate>();
		if(template.pItem.quantity < 1000000)
		{
			template.pItem.quantity++;
			template.count.text = template.pItem.quantity.ToString();
			
			SheetManager.SaveActiveCharacter();
		}
	}
	
	void AddItems(List<string> items)
	{
		foreach(string itemName in items)
		{
			string n = itemName.TrimEnd(new char[]{'^','$','@'});
			if(ItemManager.index.ContainsKey(n))
			{
				SheetManager.activeCharacter.inventories.Add(ItemManager.Load(n));
			}
			else
			{
				SheetManager.activeCharacter.weapons.Add(WeaponManager.Load(n));
			}
			
			SheetManager.SaveActiveCharacter();
			ToggleItemDisplays();
			ToggleItemDisplays();
		}
	}
}
